package org.dxy.trigger.http;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dxy.api.dto.request.factory.CreateFactoryRequest;
import org.dxy.api.dto.request.factory.UpdateFactoryRequest;
import org.dxy.api.dto.response.R;
import org.dxy.infrastructure.persistent.po.query.FactoryViewPo;
import org.dxy.trigger.http.service.FactoryApplicationService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工厂管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/factories")
@RequiredArgsConstructor
public class FactoryController {
    
    private final FactoryApplicationService factoryApplicationService;
    
    /**
     * 创建工厂
     */
    @PostMapping("/create")
    public R<Long> createFactory(@Validated @RequestBody CreateFactoryRequest request) {
        return factoryApplicationService.create(request);
    }
    
    /**
     * 分页查询工厂列表
     */
    @GetMapping("page")
    public R<Page<FactoryViewPo>> getFactoryPage(
            @ParameterObject Page<FactoryViewPo> page,
            @ParameterObject FactoryViewPo query) {
        return factoryApplicationService.page(page, query);
    }
    
    /**
     * 更新工厂
     */
    @PutMapping("/{id}")
    public R<Void> updateFactory(@PathVariable Long id, @Validated @RequestBody UpdateFactoryRequest request) {
        return factoryApplicationService.update(id, request);
    }
    
    /**
     * 删除工厂
     */
    @DeleteMapping("/{id}")
    public R<Void> deleteFactory(@PathVariable Long id) {
        return factoryApplicationService.delete(id);
    }
    
    /**
     * 获取工厂详情
     */
    @GetMapping("/{id}")
    public R<FactoryViewPo> getFactory(@PathVariable Long id) {
        return factoryApplicationService.getById(id);
    }
    
    /**
     * 启用工厂
     */
    @PutMapping("/{id}/enable")
    public R<Void> enableFactory(@PathVariable Long id) {
        return factoryApplicationService.enable(id);
    }
    
    /**
     * 禁用工厂
     */
    @PutMapping("/{id}/disable")
    public R<Void> disableFactory(@PathVariable Long id) {
        return factoryApplicationService.disable(id);
    }
    
    /**
     * 获取启用状态的工厂列表
     */
    @GetMapping("/status/enabled")
    public R<List<FactoryViewPo>> getEnabledFactories() {
        return factoryApplicationService.getEnabledFactories();
    }


}